<template>
  <div>
    <el-row>
      <el-col>
        <el-table
          :data="moneyList.slice((page-1)*pageSize,pageSize*page)"
          style="width: 100%"
          class="tableinfo margintop20"
          :header-cell-style="{'text-align':'center'}"
          :cell-style="{'text-align':'center'}"
          stripe
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="名字" type="username" prop="username"></el-table-column>
          <el-table-column label="职位" prop="roleid">
            <template slot-scope="scope">
              <el-tag
                effect="plain"
                :type="scope.row.roleid | colortype"
              >{{scope.row.roleid | rolename}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
              <el-tag
                effect="plain"
                :type="scope.row.status | colortype"
              >{{scope.row.status | statusname}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="底资" prop="money"></el-table-column>
          <el-table-column label="提成" prop="sales1"></el-table-column>
          <el-table-column label="应发金额" prop="salary">
            <template slot-scope="scope">{{scope.row.money *1+ scope.row.sales1 *1}}</template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                @click="editAdvise(scope.row)"
                v-if="roleid*1==4 "
                size="mini"
                type="success"
              >编辑</el-button>
              <!-- <el-popconfirm
                confirmButtonText="好的"
                cancelButtonText="不用了"
                icon="el-icon-info"
                iconColor="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="delUser(scope.row)"
              >
                <el-button size="mini" slot="reference" type="danger" v-if="roleid*1==4">删除</el-button>
              </el-popconfirm> -->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-dialog title="修改薪资" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form
        :model="form"
        :rules="rules"
        ref="myform"
        label-width="100px"
        label-position="right"
        class="myform"
      >
        <el-form-item label="底薪：" prop="money">
          <el-input type="money" v-model="form.money"></el-input>
        </el-form-item>
        <el-form-item label="提成：" prop="sales1">
          <el-input type="money" v-model="form.sales1"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateById()">确 定</el-button>
      </span>
    </el-dialog>

    <div class="flexbox">
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="changeCurrent"
        :page-size="pageSize"
        :current-page="page"
        :total="total"
      ></el-pagination>
      <span>共 {{total}} 条数据</span>
    </div>
  </div>
</template>

<script>
import { moneylist, delmoney, updatemoney, getuserlist } from "@/api";
import { roleList, statusList } from "@/utils";
import Vue from "vue";
var roleid = sessionStorage.getItem("roleid");
export default {
  data() {
    return {
      moneyList: [],
      page: 1,
      pageSize: 6,
      roleid,
      dialogVisible: false,
      form: {},
      rules: {
        money: [
          { required: true, message: "请选择用户角色类型", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    total() {
      return this.moneyList.length;
    },
  },
  methods: {
    changeCurrent(page) {
      this.page = page;
    },
    updateById() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          updatemoney(this.form).then((res) => {
            if (res.type) {
              this.dialogVisible = false;

              console.log(this.form.salary);
              this.moneyList.forEach((item, i) => {
                if (item._id == this.form._id) {
                  Vue.set(this.moneyList, i, this.form);
                }
              });
            }
          });
        } else {
          this.$message.error("请输入修改内容...");
          return false;
        }
      });
    },

    handleClose() {
      this.dialogVisible = false;
    },
    editAdvise(row) {
      if (row.status * 1 == 2) {
        this.$message.error("此用户已被停用..");
      } else {
        this.form = { ...this.form, ...row };
        this.dialogVisible = true;
      }
    },
    // delUser(row) {
    //   delmoney({
    //     roleid: row.roleid,
    //   }).then((res) => {
    //     if (res.type) {
    //       this.moneyList = this.moneyList.filter(
    //         (item) => item.roleid != row.roleid
    //       );
    //     }
    //   });
    // },
  },
  mounted() {
    getuserlist().then((res) => {
      if (res.type) {
        this.moneyList = res.result;
        console.log(this.moneyList);
      }
    });
  },
};
</script>